﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DukeySoundHelper.API;
using DukeySoundHelper.Properties;

namespace DukeySoundHelper
{
    public partial class SetConfig : Form
    {
        public  static SetConfig instance = new SetConfig();
        public SetConfig()
        {
            if (DukeyComm.lang == "english")
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); 
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            DukeyComm.AutoStart = chkAutoStart.Checked;
            int waitseconds=4;
            int.TryParse(txtWaitSeconds.Text.Trim(),out  waitseconds);
            DukeyComm.waitseconds=waitseconds;
            DukeyComm.backcolor = txtBackColor.Text.Trim();

            int left = -1;
            int.TryParse(txtwinpopLeft.Text.Trim(), out left);
            DukeyComm.WinPopLeft = left;

            int top = -1;
            int.TryParse(txtwinpopTop.Text.Trim(), out top);
            DukeyComm.WinPopTop = top;

            
            Settings.Default.autostart = DukeyComm.AutoStart;
            Settings.Default.ChangeUnit = DukeyComm.ChangeUnit;
            Settings.Default.IsMouseLeft = DukeyComm.IsMouseLeft;
            Settings.Default.IsMouseRight = DukeyComm.IsMouseRight;
            Settings.Default.Highkey = DukeyComm.Highkey;
            Settings.Default.Lowkey = DukeyComm.Lowkey;
            Settings.Default.backcolor = DukeyComm.backcolor;
            Settings.Default.waitseconds = DukeyComm.waitseconds;
            Settings.Default.WinPopLeft = DukeyComm.WinPopLeft;
            Settings.Default.WinPopTop = DukeyComm.WinPopTop;
            bool isneedrestart = false;
            if(DukeyComm.lang!=cboxLang.Text)
            {
                isneedrestart = true;
                DukeyComm.lang = cboxLang.Text;
                Settings.Default.lang = DukeyComm.lang;
            }
            Settings.Default.Save();
            
            this.DialogResult = DialogResult.OK;

            if (isneedrestart)
            {
                int nExitCode = 0;//结束状态
               
            }
        }

        private void SetConfig_Load(object sender, EventArgs e)
        {
            chkAutoStart.Checked = DukeyComm.AutoStart;
            txtWaitSeconds.Text = DukeyComm.waitseconds.ToString();
            txtBackColor.Text = DukeyComm.backcolor;
            txtwinpopLeft.Text = DukeyComm.WinPopLeft.ToString();
            txtwinpopTop.Text = DukeyComm.WinPopTop.ToString();
            if (DukeyComm.WinPopLeft == -1 && DukeyComm.WinPopTop == -1)
            {
                txtwinpopLeft.Text = (Screen.PrimaryScreen.WorkingArea.Width / 2 - 300).ToString();
                txtwinpopTop.Text = (Screen.PrimaryScreen.WorkingArea.Height / 2 - 47).ToString();
            }
            cboxLang.Text = DukeyComm.lang;
        }

        private void txtBackColor_MouseClick(object sender, MouseEventArgs e)
        {
            if (DialogResult.OK == colorDialog1.ShowDialog())
            {
                txtBackColor.Text = DukeyComm.ColorToHex(colorDialog1.Color);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtwinpopLeft.Text = (Screen.PrimaryScreen.WorkingArea.Width / 2 - 300).ToString();
            txtwinpopTop.Text ="0";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtwinpopLeft.Text = (Screen.PrimaryScreen.WorkingArea.Width / 2 - 300).ToString();
            txtwinpopTop.Text = (Screen.PrimaryScreen.WorkingArea.Height / 2 - 47).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            txtwinpopLeft.Text = (Screen.PrimaryScreen.WorkingArea.Width / 2 - 300).ToString();
            txtwinpopTop.Text = (Screen.PrimaryScreen.WorkingArea.Height  - 94).ToString();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            RegisterFrm frm = new RegisterFrm();
            frm.ShowDialog();
        }
    }
}
